package com.baimifan.renovation.manager;

import android.os.Handler;
import android.os.Message;

import com.baimifan.renovation.MyApplication;
import com.baimifan.renovation.model.FindData;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;

/**
 * Created by baimifan on 2017/07/05.
 */

public class FindManager {
    private Handler mHandler;
    private String url = MyApplication.MAIN + "/index/faxian/read";
    private ArrayList<FindData> lists = new ArrayList<>();

    public ArrayList<FindData> getFind () {
        return lists;
    }

    public FindManager (Handler handler) {
        this.mHandler = handler;
    }


    public void finddata (String id) {
        RequestParams params = new RequestParams(url);
        params.addBodyParameter("id", id);
        x.http().post(params, new Callback.CommonCallback<JSONObject>() {
            @Override
            public void onSuccess(JSONObject result) {
                try {
                    System.out.println("--datas"  );
                    if (result.getInt("status") == 200) {
                        JSONArray array = result.getJSONArray("data");
                        for (int i = 0; i < array.length(); i++) {
                            JSONObject data = array.getJSONObject(i);
                            FindData finddata = new FindData();
                            if (!data.isNull("zl_id")) {
                                finddata.setZlId(data.getInt("zl_id"));
                            }
                            if (!data.isNull("zl_pic")) {
                                if (!data.isNull("zl_type")) {
                                    if (data.getInt("zl_type") == 0) {
                                        finddata.setZlPic(MyApplication.MAIN + data.getString("zl_pic"));
                                    } else {
                                        JSONArray pic_array = data.getJSONArray("zl_pic");
                                        String [] pics = new String[pic_array.length()];
                                        for (int z = 0; z < pic_array.length(); z ++) {
                                            pics[z] = MyApplication.MAIN + pic_array.getString(z);
                                        }
                                        finddata.setZlPicArray(pics);
                                    }
                                }
                            }
                            if (!data.isNull("zl_type")) {
                                finddata.setZlType(data.getInt("zl_type"));
                            }
                            if (!data.isNull("create_time")) {
                                finddata.setCreateTime(data.getString("create_time"));
                            }
                            if (!data.isNull("zl_title")) {
                                finddata.setZlTitle(data.getString("zl_title"));
                            }
                            lists.add(finddata);
                        }
                        Message msg = new Message();
                        msg.what = 7;
                        mHandler.sendMessage(msg);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
            }
        });
    }
}
